<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@include file="/commons/taglibs.jsp"%>
<script type="text/javascript" src="<%=request.getContextPath() %>/lib/uplodifyh5/event_common.js"></script>
<style>
<!--
 上传进度样式
-->
#progress_info table td{
	padding:8px;
}
#progress_info table{
	color: white;
	margin: 0 auto;
	border: 1px solid #9f9;
}
#progress_info>p:last-child{
	font-size:22px;
	animation:light 5s infinite;
	-moz-animation:light 5s infinite;
	-webkit-animation:light 5s infinite;
	-o-animation:light 5s infinite;
}
#progress_info,#progress_info>p{
	transition:all 1s;
    -webkit-transition:all 1s;
}
@keyframes light{
	0%{text-shadow:#59b8dc 1px 0 0,#59b8dc 0 1px 0,#59b8dc -1px 0 0,#59b8dc 0 -1px 0;}
	30%{text-shadow:transparent 1px 0 0,transparent 0 1px 0,transparent -1px 0 0,transparent 0 -1px 0;}
	100%{text-shadow:#59b8dc 1px 0 0,#59b8dc 0 1px 0,#59b8dc -1px 0 0,#59b8dc 0 -1px 0;}}
@-webkit-keyframes light{
	0%{text-shadow:#59b8dc 1px 0 0,#59b8dc 0 1px 0,#59b8dc -1px 0 0,#59b8dc 0 -1px 0;}
	30%{text-shadow:transparent 1px 0 0,transparent 0 1px 0,transparent -1px 0 0,transparent 0 -1px 0;}
	100%{text-shadow:#59b8dc 1px 0 0,#59b8dc 0 1px 0,#59b8dc -1px 0 0,#59b8dc 0 -1px 0;}}
@-moz-keyframes light{
	0%{text-shadow:#59b8dc 1px 0 0,#59b8dc 0 1px 0,#59b8dc -1px 0 0,#59b8dc 0 -1px 0;}
	30%{text-shadow:transparent 1px 0 0,transparent 0 1px 0,transparent -1px 0 0,transparent 0 -1px 0;}
	100%{text-shadow:#59b8dc 1px 0 0,#59b8dc 0 1px 0,#59b8dc -1px 0 0,#59b8dc 0 -1px 0;}}
}



</style>
<!-- strat form -->
<!--<form name="userListForm" id="${formId}" target="_self"
	class="form-horizontal searchCondition">
	<div id="userManagerQueryDivId" hidden="true">
		<table border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td>
					<div class="form-group">
						<label class="control-label" style="width: 100px; float: left;">用户名称:&nbsp;</label>
						<div style="margin-left: 15px; float: left;">
							<input name="name" class="form-control" type="text"
								style="width: 180px;" id="nameID" dataType="Chinese"
								require="false" />
						</div>
					</div>
				</td>
				<td>
					<div class="form-group">
						<label class="control-label" style="width: 100px; float: left;">用户帐号:&nbsp;</label>
						<div style="margin-left: 15px; float: left;">
							<input name="userAccount" class="form-control" type="text"
								style="width: 180px;" id="descID" />
						</div>
					</div>
				</td>
				<td></td>
			</tr>
			<tr>
				<td>
					<div class="form-group">
						<label class="control-label" style="width: 100px; float: left;">用户描述:&nbsp;</label>
						<div style="margin-left: 15px; float: left;">
							<input name="description" class="form-control" type="text"
								style="width: 180px;" />
						</div>
					</div>
				</td>
				<td>
					<div class="form-group">
						<label class="control-label" style="width: 100px; float: left;">用户角色:&nbsp;</label>
						<div style="margin-left: 15px; float: left;">
							<input name="role" class="form-control" type="text"
								style="width: 180px;" id="roleID" />
						</div>
					</div>
				</td>
				<td> 
				<button id="userManagerSearch" type="button" style="position:relative; margin-left:35px; top: -8px" class="btn btn-success glyphicon glyphicon-search">查询</button>
				</td>
			</tr>
		</table>
	</div>
</form>-->
<!-- end form -->
<body style="min-width: 300px;">
<!-- form搜索布局换位栅格布局 -->
<div  class="container-fulid" style="width:98%;margin-right:auto; margin-left:auto; padding-top: 15px;">
<form name="userListForm" id="${formId}" target="_self"
	class="form-horizontal searchCondition">
<div class="container-fluid" id="userManagerQueryDivId" hidden="true">
	<div class="row">
		<div class="form-group col-md-4 col-md-offset-2 col-sm-4 col-sm-offset-2">
			<label class="control-label" style="width: 100px; float: left;">用户名称:&nbsp;</label>
			<div style="margin-left: 15px; float: left;">
				<input name="name" class="form-control" type="text" style="width: 180px;" id="nameID" dataType="Chinese" require="false" />
			</div>
		</div>
		<div class="form-group col-md-4 col-sm-4">
			<label class="control-label" style="width: 100px; float: left;">用户帐号:&nbsp;</label>
			<div style="margin-left: 15px; float: left;">
				<input name="userAccount" class="form-control" type="text"
					style="width: 180px;" id="descID" />
			</div>
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-4 col-md-offset-2 col-sm-4 col-sm-offset-2">
			<label class="control-label" style="width: 100px; float: left;">用户描述:&nbsp;</label>
			<div style="margin-left: 15px; float: left;">
				<input name="description" class="form-control" type="text"
					style="width: 180px;" />
			</div>
		</div>
		<div class="form-group col-md-5 col-sm-5">
			<label class="control-label" style="width: 105px; float: left;">用户角色:&nbsp;</label>
			<div style="margin-left: 15px; float: left;">
				<input name="role" class="form-control" type="text"
					style="width: 180px;" id="roleID" />&nbsp;&nbsp;
				<button id="userManagerSearch" type="button" style="margin-left:5px;height:33px;position: relative;" class="btn btn-success glyphicon glyphicon-search">查询</button>
			</div>
		</div>
	</div>
</div>
</form>
</div>

<!-- end栅格布局 -->

<div><input id="userImport" type="file" name="fileuserimport" style="display: none;"/></div>
<div data-role="userGrid">
</div>
</body>
	<script>
	$(function (){	
		
		
		tabId=$("#tabContent > .tab-pane.active").attr("id");
		$("#navTabs").find("li").each(function(i){
			$(this).on("click", function () {
				tabId=$("#tabContent > .tab-pane.active").attr("id");
	         });
		});
		
		PageLoader = {
		    initSearchPanel:function(){},
		    initGridPanel: function(){
		         var self = this;
		         var url = contextPath + '/auth/user/pagingQuery.dhc';
		         var getButtons = function() {
                    return [{
                        content : '<ks:hasSecurityResource identifier="userManagerAdd"><button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-plus"><span>添加</button></ks:hasSecurityResource>',
                        action : 'add'
                    }, {
                        content : '<ks:hasSecurityResource identifier="userManagerUpdate"><button class="btn btn-edit"  type="button"><span class="glyphicon glyphicon-edit"><span>修改</button></ks:hasSecurityResource>',
                        action : 'modify'
                    }, {
                        content : '<ks:hasSecurityResource identifier="userManagerImport"><button class="btn  btn-darkblue"  type="button"><span class="glyphicon glyphicon-cloud-upload"><span>用户批量导入</button></ks:hasSecurityResource>',
                        action : 'import'
                    }, {
                        content : '<ks:hasSecurityResource identifier="userManagerTerminate"><button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove"><span>删除</button></ks:hasSecurityResource>',
                        action : 'delete'
                    }, {
                        content : '<ks:hasSecurityResource identifier="userManagerGrantRole"><button class="btn btn-orange"   type="button"><span class="glyphicon glyphicon-th-large"></span>&nbsp;分配角色</button></ks:hasSecurityResource>',
                        action : 'grantRoleToUser'
                    }, {
                        content : '<button class="btn btn-pearlblue"  type="button"><span class="glyphicon glyphicon-th-large"></span>&nbsp;授权科室</button>',
                        action : 'grantDepToUser'
                    },{
                        content : '<button class="btn btn-info" type="button"><span class="glyphicon glyphicon-th-large"></span>&nbsp;授权病区</button>',
                        action : 'grantWardToUser'
                    },/*  {
                        content : '<button class="btn btn-info" type="button"><span class="glyphicon glyphicon-th-large"></span>&nbsp;分配指标</button>',
                        action : 'attrQuotas'
                    },  */{
                        content : '<ks:hasSecurityResource identifier="userManagerGrantPermission"><button class="btn btn-info" type="button"><span class="glyphicon glyphicon-th-large"></span>&nbsp;分配权限</button></ks:hasSecurityResource>',
                        action : 'grantPermissionToUser'
                    }, {
                        content : '<ks:hasSecurityResource identifier="userManagerResetPassword"><button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-wrench"></span>&nbsp;重置密码</button></ks:hasSecurityResource>',
                        action : 'resetPassword'
                    },{
                        content : '<ks:hasSecurityResource identifier="userManagerSuspend"><button class="btn btn-dark"  type="button"><span class="glyphicon glyphicon-wrench"></span>&nbsp;禁用</button></ks:hasSecurityResource>',
                        action : 'forbidden'
                    },{
                        content : '<ks:hasSecurityResource identifier="userManagerActivate"><button class="btn btn-success" type="button"><span class="glyphicon glyphicon-wrench"></span>&nbsp;激活</button></ks:hasSecurityResource>',
                        action : 'available'
                    },{
                        content : '<ks:hasSecurityResource identifier="userManagerQuery"><button class="btn btn-lightgreen"   type="button"><span class="glyphicon glyphicon-search"></span>&nbsp;高级搜索&nbsp; <span class="caret"></span> </button></ks:hasSecurityResource>',
                        action : 'userManagerQuery'
                    }];
		 		};
		         return $('[data-role="userGrid"]').off().grid({
		                identity:"id",
		                buttons: getButtons(),
		                url:url,
		                columns: [{
							title : "用户帐号",
							name : "userAccount",
							width : 100
						},{
							title : "用户名称",
							name : "name",
							width : 100
						}, {
							title : "授权角色",
							name : "roles",
							width : 100
						},  {
							title : "创建人",
							name : "createOwner",
							width : 100
						},   {
							title : "用户描述",
							name : "description",
							width : 150
						},  {
							title : "是否有效",
							name : "disabled",
							width : 80,
							 render : function(item, name, index) {
								return item[name]?  '<span class="glyphicon glyphicon-remove" style="color:#D9534F;margin-left:15px;"></span>':'<span class="glyphicon glyphicon-ok" style="color:#5CB85C;margin-left:15px;"></span>';
							}  
						},  {
							title : "有效截止日期",
							name : "validityDate",
							width : 150,
							 render : function(item, name, index) {
								    var aa=item[name];
									var dt = new Date(aa).Format("yyyy-MM-dd"); 
									return dt;
								}  
						
						}, {
							title : "查看",
							name : "operate",
							width : 200,
							 render: function(item, name, index){
					             	return '<a href="#" onclick="showUserDetail('+item.id+', \''+item.name+'\')"><span class="glyphicon glyphicon glyphicon-eye-open"></span>&nbsp;详细</a>';
					        	 }
						}
		                ]
		         }).on({
		 			'add' : function() {
						userManager().add($(this));
					},
					'modify' : function(event, data) {
						var indexs = data.data;
						var $this = $(this);
						if (indexs.length == 0) {
							drgUI.warning('请选择一条记录进行修改');
							return;
						}
						if (indexs.length > 1) {
							drgUI.warning('只能选择一条记录进行修改');
							return;
						}
						userManager().modify(data.item[0], $(this));
					},
					'import' : function(event, data) {
						$('#'+tabId+' #uploadifive-userImport input[type="file"]:last').click() ;
					},
					'delete' : function(event, data) {
						var indexs = data.data;
						var $this = $(this);

                        if (indexs.length == 0) {
                        	drgUI.warning('请选择要删除的记录');
                            return;
						}
						drgUI.confirm({
							content : '确定要删除所选记录吗?',
							callSure : function() {
								userManager().deleteUser(data.item, $this);
							}
						});
					},
					'resetPassword' : function(event, data) {
						var indexs = data.data;
						var $this = $(this);
						if (indexs.length == 0) {
							drgUI.warning('请选择一条记录进行修改');
							return;
						}
						userManager().resetPassword(data.data, $(this));
					},
					'forbidden' : function(event, data) {
						var indexs = data.data;
						var $this = $(this);
						if (indexs.length == 0) {
							drgUI.warning('请选择一条记录进行修改');
							return;
						}
						if (indexs.length > 1) {
							drgUI.warning('只能选择一条记录进行修改');
							return;
						}
						
						userManager().forbidden(data.item[0] , $this);
					},
					'userManagerQuery' : function() {
						$("#userManagerQueryDivId").slideToggle("slow");
						
					},
					'available' : function(event, data) {
						var indexs = data.data;
						var $this = $(this);
						if (indexs.length == 0) {
							drgUI.warning('请选择一条记录进行修改');
							return;
						}
						if (indexs.length > 1) {
							drgUI.warning('只能选择一条记录进行修改');
							return;
						} 
						userManager().available(data.item[0] , $this);
				    },
                    'grantRoleToUser' : function(event, data) {
                        var indexs = data.data;
                        var $this = $(this);
                        if (indexs.length == 0) {
                        	drgUI.warning('请选择一条记录进行修改');
                            return;
                        }
                        if (indexs.length > 1) {
							drgUI.warning('只能选择一条记录进行修改');
                            return;
                        }

                        var userId = data.item[0].id;
                        var userAccount = data.item[0].userAccount;

                        $.get(contextPath + '/pages/auth/user-grantAuthorityToUser.jsp').done(function(data){
                            var dialog  = $(data);

                            dialog.find('.modal-header').find('.modal-title').html('为用户[' + userAccount + ']授权角色');

                            dialog.find('#grantAuthorityToUserButton').on('click',function(){
                                var grantRolesToUserTableItems = dialog.find('#notGrantAuthoritiesToUserGrid').getGrid().selectedRows();

                                if(grantRolesToUserTableItems.length == 0){
                                	drgUI.warning('请选择要需要被授权的角色');        
                                    return;
                                }

                                var data = "userId="+userId;
                                $.each(grantRolesToUserTableItems,function(index,grantRolesToUserTableItem){
                                    data += ("&roleIds=" + grantRolesToUserTableItem.id + "&");
                                });
                                data = data.substring(0, data.length-1);
                                $.post(contextPath + '/auth/user/grantRolesToUser.dhc?time=' + new Date().getTime(), data).done(function(data) {
                                    if(data.success){
                                        drgUI.success('为用户授权角色成功');   
                                        dialog.find('#notGrantAuthoritiesToUserGrid').grid('refresh');
                                        dialog.find('#grantAuthoritiesToUserGrid').grid('refresh');
                                    }
                                });
                            });

                            dialog.find('#notGrantAuthorityToUserButton').on('click',function(){
                                var notGrantRolesToUserGridItems = dialog.find('#grantAuthoritiesToUserGrid').getGrid().selectedRows();

                                if(notGrantRolesToUserGridItems.length == 0){
                                   drgUI.warning('请选择要需要删除的角色');
                                    return;
                                }

                                var data = "userId=" + userId;
                                $.each(notGrantRolesToUserGridItems,function(index,notGrantRolesToUserGridItem){
                                    data += ("&roleIds=" + notGrantRolesToUserGridItem.id + "&");
                                });
                                data = data.substring(0, data.length-1);
                                $.post(contextPath + '/auth/user/terminateAuthorizationByUserInRoles.dhc', data).done(function(data) {
                                    if(data.success){
                                        drgUI.warning('删除用户的角色成功！');
                                        dialog.find('#notGrantAuthoritiesToUserGrid').grid('refresh');
                                        dialog.find('#grantAuthoritiesToUserGrid').grid('refresh');
                                    }
                                });

                            });
                            
                            dialog.modal({
                                keyboard: false,
                                backdrop: false
                            }).on({
                                'hidden.bs.modal' : function(){
                                	$this.grid('refresh');
                                    $(this).remove();
                                },
                                'shown.bs.modal' : function(){
                                
                                }

                            });
                            var columns = [{
                                title : "角色名称",
                                name : "name",
                                width : 100
                            }, {
                                title : "角色描述",
                                name : "description",
                                width : 100
                            }];
                            dialog.find('#notGrantAuthoritiesToUserGrid').grid({
                                identity: 'id',
                                columns: columns,
                                url: contextPath + '/auth/user/pagingQueryNotGrantRoles.dhc?userId='+userId+'&time='+new Date().getTime()
                            });

                            dialog.find('#grantAuthoritiesToUserGrid').grid({
                                identity: 'id',
                                columns: columns,
                                url: contextPath + '/auth/user/pagingQueryGrantRoleByUserId.dhc?userId='+userId
                            });
                        });

                    },
                    /* 病区授权 */
                    'grantWardToUser' : function(event, data) {
                        var indexs = data.data;
                        var $this = $(this);
                        if (indexs.length == 0) {
                            drgUI.warning('请选择一条记录授权病区');
                            return;
                        }
                        if (indexs.length > 1) {
                            drgUI.warning('只能选择一条记录授权病区');
                            return;
                        }
                        var roles =  data.item[0].roles;
                        if(roles.indexOf("病区") < 0){
                        	drgUI.warning('只能为病区角色的用户授权病区');
                            return;
                        }
                        var userId = data.item[0].id;
                        var userAccount = data.item[0].userAccount;
                        $.get(contextPath + '/pages/auth/user-grantWardToUser.jsp').done(function(data){
                            var dialog  = $(data);
                            dialog.find('.modal-header').find('.modal-title').html('为用户[' + userAccount + ']授权病区');
                            dialog.find('.modal-body').find('.left-modal-body').html('已授权病区');
                            dialog.find('.modal-body').find('.right-modal-body').html('未授权病区');
                           
                            dialog.find('#grantWardToUserButton').on('click',function(){
                                var selectRows = dialog.find('#notGrantWardToUserGrid').getGrid().selectedRows();
                                var selectedRows = dialog.find('#grantWardToUserGrid').getGrid().items;
                                if(selectRows.length == 0){
                                   	drgUI.warning('请选择要需要被授权的病区');
                                    return;
                                }
                                var data = "userId="+userId;
                                data += "&wards="
                                $.each(selectRows,function(index,grantRolesToUserTableItem){
                                    data += grantRolesToUserTableItem.wardCode + ",";
                                });
                                $.each(selectedRows,function(index,a){
                                    data += a.wardCode + ",";
                                });
                                data = data.substring(0, data.length-1);
                                $.post(contextPath + '/auth/user/grantWardsToUser.dhc?time=' + new Date().getTime(), data).done(function(data) {
                                   if(data.success){
                                       drgUI.success('为用户授权病区成功');
                                       dialog.find('#notGrantWardToUserGrid').grid('refresh');
                                       dialog.find('#grantWardToUserGrid').grid('refresh');
                                   }
                                });
                            });

                            dialog.find('#notGrantWardToUserButton').on('click',function(){
                                var selectedRows = dialog.find('#grantWardToUserGrid').getGrid().selectedRows();
                                if(selectedRows.length == 0){
                                    drgUI.warning('请选择要需要删除的病区');
                                    return;
                                }

                                var data = "userId=" + userId;
                                data += "&wards=";
                                    $.each(selectedRows,function(index,a){
                                        data += a.wardCode + ",";
                                    });
                                
                                data = data.substring(0, data.length-1);
                                $.post(contextPath + '/auth/user/terminateAuthorizationByUserInWards.dhc', data).done(function(data) {
                                    if(data.success){
                                        drgUI.success('删除用户的病区成功！');
                                        dialog.find('#notGrantWardToUserGrid').grid('refresh');
                                        dialog.find('#grantWardToUserGrid').grid('refresh');
                                    }
                                });

                            });
                            dialog.modal({
                                keyboard: false,
                                backdrop: false
                            }).on({
                                'hidden.bs.modal' : function(){
                                    $(this).remove();
                                },
                                'shown.bs.modal' : function(){
                                
                                }
                            });
                            var columns = [{
                                title : "",
                                name : "wardDesc",
                                width : 150
                            }];
                            dialog.find('#notGrantWardToUserGrid').grid({
                                identity: 'id',
                                columns: columns,
                                isShowPages: false,
                                pageSize:10000000,
                                pageNo:0,
                                pagination:false,
                                url: contextPath + '/auth/user/pagingQueryNotGrantWards.dhc?userId='+userId+'&time='+new Date().getTime()
                            });
                            dialog.find('#grantWardToUserGrid').grid({
                                identity: 'id',
                                columns: columns,
                                isShowPages: false,
                                pageSize:10000000,
                                pageNo:0,
                                pagination:false,
                                url: contextPath + '/auth/user/pagingQueryWardsByUserId.dhc?userId='+userId
                            });
                            dialog.find('#notGrantWardToUserGrid').find('tbody .grid-table-head tr th:eq(1)').html('<input id ="depname_id" type="text" style="height: 30px;" placeholder="请输入病区名称回车">');
                            dialog.find('#notGrantWardToUserGrid').find('tbody .grid-table-head tr th:eq(1) input').keydown(function(event){ 
                            	if(event.keyCode==13){ 
                            		dialog.find('#notGrantWardToUserGrid').getGrid().search({wardName:$(this).val()});
                            	}
                            }); 
                        });

                    },
                     /* 科室授权 */
                    'grantDepToUser' : function(event, data) {
                        var indexs = data.data;
                        var $this = $(this);
                        if (indexs.length == 0) {
                            drgUI.warning('请选择一条记录授权科室！');
                            return;
                        }
                        if (indexs.length > 1) {
                            drgUI.warning('只能选择一条记录授权科室！');
                            return;
                        }
                        var roles =  data.item[0].roles;
                        if(roles.length == 0){
                        	drgUI.warning('此用户没有分配角色！');
                            return;
                        }
                        if(roles.indexOf("科室") < 0){
                        	drgUI.warning('只能为医保角色或科室角色的用户授权科室！');
                            return;
                        }
                        var userId = data.item[0].id;
                        var userAccount = data.item[0].userAccount;

                        $.get(contextPath + '/pages/auth/user-grantDepToUser.jsp').done(function(data){
                            var dialog  = $(data);
                            dialog.find('.modal-header').find('.modal-title').html('为用户[' + userAccount + ']授权科室');
                            dialog.find('.modal-body').find('.left-modal-body').html('已授权科室');
                            dialog.find('.modal-body').find('.right-modal-body').html('未授权科室');

                            dialog.find('#grantDepToUserButton').on('click',function(){
                                var grantRolesToUserTableItems = dialog.find('#notGrantDepsToUserGrid').getGrid().selectedRows();
                                var grantDepsToUserGrid = dialog.find('#grantDepsToUserGrid').getGrid().items;
                                if(grantRolesToUserTableItems.length == 0){
                       				drgUI.warning('请选择要需要被授权的科室!');
                                    return;
                                }

                                var data = "userId="+userId;
                                data += "&depCodes=";
                                $.each(grantRolesToUserTableItems,function(index,grantRolesToUserTableItem){
                                	if(index==0){
                                		data += grantRolesToUserTableItem.depCode;
                               		}else{
                                		data += ","+grantRolesToUserTableItem.depCode;
                               		}
                                });
                                $.each(grantDepsToUserGrid,function(index,a){
                                	data += ","+a.depCode;
                                });
                               
                                $.post(contextPath + '/auth/user/grantDepsToUser.dhc?time=' + new Date().getTime(), data).done(function(data) {
                                   if(data.success){
                                       drgUI.success('为用户授权科室成功!');
                                       dialog.find('#notGrantDepsToUserGrid').grid('refresh');
                                       dialog.find('#grantDepsToUserGrid').grid('refresh');
                                   }
                                });
                            });

                            dialog.find('#notGrantDepToUserButton').on('click',function(){
                                var grantDepsToUserGrid = dialog.find('#grantDepsToUserGrid').getGrid().selectedRows();
                                if(grantDepsToUserGrid.length == 0){
                                    drgUI.warning('请选择要需要删除的科室!');
                                    return;
                                }

                                var data = "userId=" + userId;
                                data += "&depCodes="
                                    $.each(grantDepsToUserGrid,function(index,a){
                                    	if(index==0){
                                    		data += a.depCode;
                                   		}else{
                                    		data += ","+a.depCode;
                                   		}
                                    });
                                $.post(contextPath + '/auth/user/terminateAuthorizationByUserInDeps.dhc', data).done(function(data) {
                                    if(data.success){
                                        drgUI.success('删除用户的科室成功！');
                                        dialog.find('#notGrantDepsToUserGrid').grid('refresh');
                                        dialog.find('#grantDepsToUserGrid').grid('refresh');
                                    }
                                });

                            });
                            
                            dialog.modal({
                                keyboard: false,
                                backdrop: false
                            }).on({
                                'hidden.bs.modal' : function(){
                                    $(this).remove();
                                },
                                'shown.bs.modal' : function(){
                                
                                }

                            });
                            var columns = [{
                                title : "",
                                name : "depName",
                                width : 150
                            }];
                            dialog.find('#notGrantDepsToUserGrid').grid({
                                identity: 'id',
                                columns: columns,
                                isShowPages: false,
                                pageSize:10000000,
                                pageNo:0,
                                pagination:false,
                                url: contextPath + '/auth/user/pagingQueryNotGrantDeps.dhc?userId='+userId+'&time='+new Date().getTime()
                            });

                            dialog.find('#grantDepsToUserGrid').grid({
                                identity: 'id',
                                columns: columns,
                                isShowPages: false,
                                pageSize:10000000,
                                pageNo:0,
                                pagination:false,
                                url: contextPath + '/auth/user/pagingQueryDepsByUserId.dhc?userId='+userId
                            });
                            dialog.find('#notGrantDepsToUserGrid').find('tbody .grid-table-head tr th:eq(1)').html('<input id ="depname_id" type="text" style="height: 30px;" placeholder="请输入科室名称回车">');
                            dialog.find('#notGrantDepsToUserGrid').find('tbody .grid-table-head tr th:eq(1) input').keydown(function(event){ 
                            	if(event.keyCode==13){ 
                            		dialog.find('#notGrantDepsToUserGrid').getGrid().search({depName:$(this).val()});
                            	} 
                            }); 
                        });

                    },
                    'attrQuotas': function(event, data){
                    	userManager().attrQuotas(data.item, $(this), "user");
                    },
                    'grantPermissionToUser' : function(event, data){

                        var indexs = data.data;
                        var $this = $(this);
                        if (indexs.length == 0) {
                            drgUI.warning('请选择一条记录进行授权权限！');
                            return;
                        }
                        if (indexs.length > 1) {
                            drgUI.warning('只能选择一条记录进行授权权限！');
                            return;
                        }

                        var userId = data.item[0].id;
                        var userAccount = data.item[0].userAccount;

                        $.get(contextPath + '/pages/auth/user-grantAuthorityToUser.jsp').done(function(data){
                            var dialog  = $(data);

                            dialog.find('.modal-header').find('.modal-title').html('为用户['+userAccount+']授权权限');
                            dialog.find('.modal-body').find('.left-modal-body').html('已授权权限');
                            dialog.find('.modal-body').find('.right-modal-body').html('未授权权限');

                            dialog.find('#grantAuthorityToUserButton').on('click',function(){
                                var grantPermissionsToUserItems = dialog.find('#notGrantAuthoritiesToUserGrid').getGrid().selectedRows();

                                if(grantPermissionsToUserItems.length == 0){
                                    drgUI.warning('请选择要需要被授权的权限！');
                                    return;
                                }

                                var data = "userId="+userId;
                                $.each(grantPermissionsToUserItems,function(index,grantPermissionsToUserItem){
                                    data += ("&permissionIds=" + grantPermissionsToUserItem.id + "&");
                                });
                                data = data.substring(0, data.length-1);
                                $.post(contextPath + '/auth/user/grantPermissionsToUser.dhc', data).done(function(data) {
                                    if(data.success){
                                        drgUI.success('为用户授权权限成功！');
                                        dialog.find('#notGrantAuthoritiesToUserGrid').grid('refresh');
                                        dialog.find('#grantAuthoritiesToUserGrid').grid('refresh');
                                    }
                                });
                            });

                            dialog.find('#notGrantAuthorityToUserButton').on('click',function(){
                                var notGrantRolesToUserItems = dialog.find('#grantAuthoritiesToUserGrid').getGrid().selectedRows();

                                if(notGrantRolesToUserItems.length == 0){
                                    drgUI.warning('请选择要需要删除的权限！');
                                    return;
                                }

                                var data = "userId="+userId;
                                $.each(notGrantRolesToUserItems,function(index,notGrantRolesToUserItem){
                                    data += ("&permissionIds=" + notGrantRolesToUserItem.id + "&");
                                });
                                data = data.substring(0, data.length-1);
                                $.post(contextPath + '/auth/user/terminatePermissionsByUser.dhc', data).done(function(data) {
                                    if(data.success){
                                        drgUI.error('删除用户的权限成功！');
                                        dialog.find('#notGrantAuthoritiesToUserGrid').grid('refresh');
                                        dialog.find('#grantAuthoritiesToUserGrid').grid('refresh');
                                    }
                                });

                            });

                            dialog.modal({
                                keyboard: false,
                                backdrop: false
                            }).on({
                                'hidden.bs.modal' : function(){
                                    $(this).remove();
                                },
                                'shown.bs.modal' : function(){
                                    var columns = [{
                                        title : "权限名称",
                                        name : "name",
                                        width : 100
                                    }, {
                                        title : "权限标识",
                                        name : "identifier",
                                        width : 100
                                    }];

                                    dialog.find('#notGrantAuthoritiesToUserGrid').grid({
                                        identity: 'id',
                                        columns: columns,
                                        url: contextPath + '/auth/user/pagingQueryNotGrantPermissions.dhc?userId='+userId
                                    });

                                    dialog.find('#grantAuthoritiesToUserGrid').grid({
                                        identity: 'id',
                                        columns: columns,
                                        url: contextPath + '/auth/user/pagingQueryGrantPermissionByUserId.dhc?userId='+userId
                                    });
                                }

                            });

                        });
                    }
				});
		    }
		};
		PageLoader.initSearchPanel();
		PageLoader.initGridPanel();

        var form = $('#'+'${formId}');
        form.find('#userManagerSearch').on('click', function(){
            var params = {};
            form.find('.form-control').each(function(){
                var $this = $(this);
                var name = $this.attr('name');
                 if(name){
                    params[name] = $this.val();
                }
            });
            $('[data-role="userGrid"]').getGrid().search(params);
        });
        
     // 下载按钮
    	var downloadBtn = '<div class="btn-clickable">';
    	downloadBtn += '		<a href="download/userImport.xlsx" class="link text-sm" style="text-decoration:none;color:#fff">';
    	downloadBtn += '			<i class="fa fa-download"></i>&nbsp;用户批量导入模板下载';
    	downloadBtn += '		</a>';
    	downloadBtn += '</div>';
    	$('#'+tabId+' table').find("thead > tr > th").append(downloadBtn) ;
        
        // 用户上传插件初始化
        $('#'+tabId+' #userImport').uploadifive({
			'uploadScript' : contextPath + '/fileUpload/filesUpload.dhc',
	        'height': 37,
	        'width' :140,
	        'auto'  : true,//是否自动开始
	        'uploadLimit' : 10, //一次同步上传的文件数目     
	        'fileSizeLimit' : '500MB', //单个文件大小，0为无限制，可接受KB,MB,GB等单位的字符串值     
	        'queueSizeLimit' : 100, //队列中同时存在的文件个数限制
	        'fileObjName':'file',
	        'removeCompleted': false,  //是否完成后移除序列，默认为true
	        'removeTimeout' : '5',  //上传完成后队列多长时间后消失。默认 3秒    需要：removeCompleted : true,时使用
	        'buttonText' : '<i class="fa fa-folder-open fa-lg"></i>&nbsp;&nbsp;用户信息上传',
	        'fileType' : '*.xls; *.xlsx; *.xml; *.dbf',//允许上传的文件格式
	        'multi'    : true,//是否支持多文件上传
	        'method'   :'post',//提交方式
	        'debug':false,
	        //当点击文件队列中文件的关闭按钮或点击取消上传时触发
	        'onCancel': function(){ },
	      	//返回一个错误，选择文件的时候触发  
	        'onSelectError':  function(){
	        	  drgUI.error('文件上传失败！');
	        },
	      	//检测FLASH失败调用  
	        'onFallback':function(){  
	            drgUI.error('浏览器检测不到兼容版本的HTML5！');
	        }, 
	        'onUpload': function(filesToUpload) {
	        	$("#uploadifive-userImport-queue").css("display","none") ;
	        },
	        'onUploadError' : uploadify_onUploadError,
	        'onQueueComplete':function(queueData){
	        	$.post(contextPath+'/fileUpload/userImport.dhc').done(function(result){
	        		if(result.success){
		        		$('[data-role="userGrid"]').grid('refresh');
		        		drgUI.success('导入成功!');	 
		        		if(result.data != null && result.data != ""){
		        			drgUI.alert(result.data);
		        		}
	        		}else{
	        			
	        			drgUI.alert(result.errorMessage);
	        		}
	        	});
	        }
	    });
     	$("#uploadifive-userImport").css("display","none") ;  
	});

    /**
    * 显示详细信息
    * @param id
    * @param userName
    */
	var showUserDetail = function(id, userName){
        var thiz = $(this);
        var mark = id +"userDetail";
        mark = openTab('/pages/auth/user-detail.jsp', userName, mark, id);
        if (mark) {
            thiz.attr("mark", mark);
        }
	};
	
</script>
